package com.hcq.controller;

import com.common.annotation.RequireRoles;
import com.common.entity.*;
import com.hcq.service.UserServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping("/searchSomeUserByPage")
    @RequireRoles(roles = {"admin","super_admin"},index = 2)
    @ApiOperation("查询所有用户 支持 多条件 分页 排序 查询")
    public Rsp searchSomeByPage(@RequestBody PageParam<User> userPageParam,
                                @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){

        System.out.println(userId);
        System.out.println(roleSign);

        return Rsp.success(userService.searchSomeByPage(userPageParam));
    }

    @PostMapping("/searchSomeRoleByPage")
    @RequireRoles(roles = {"admin","super_admin"},index = 2)
    @ApiOperation("查询所有用户 支持 多条件 分页 排序 查询")
    public Rsp searchSomeRoleByPage(@RequestBody PageParam<Role> rolePageParam,
                                @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){

        System.out.println(userId);
        System.out.println(roleSign);

        return userService.searchSomeRoleByPage();
    }

    @RequireRoles(roles = {"super_admin","admin"},index = 2)
    @PostMapping("/addOneUser")
    public Rsp addOneUser(@RequestBody User user, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){

        System.out.println(user);
        user.setCreateUserId(userId);
        user.setUpdateUserId(userId);
        return userService.addOne(user);
    }

    @RequireRoles(roles = {"super_admin","admin"},index = 2)
    @PostMapping("/updateUser")
    public Rsp updateUser(@RequestBody User user, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){

        return userService.update(user);
    }

    @RequireRoles(roles = {"super_admin"},index = 2)
    @PostMapping("/deleteUserLogical")
    public Rsp deleteUserLogical(@RequestBody User user, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){

        return userService.deleteLogical(user);
    }

    /**
     * 登录
     * @param loginUser
     * @return
     */
    @PostMapping("/login")
    public Rsp userLogin(@RequestBody User loginUser) {
        return userService.login(loginUser);
    }

    /**
     * 登录
     * @param loginUser
     * @return
     */
    @PostMapping("/frontLogin")
    public Rsp frontLogin(@RequestBody User loginUser) {
        return userService.frontLogin(loginUser);
    }

    /**
     * 获取用户信息
     * @param userId
     * @return
     */
    @GetMapping("getUserInfo")
    public Rsp getUserInfo(@RequestHeader(value = "userId") String userId) {
        return Rsp.success(userService.getOneById(userId));
    }

    /**
     * 注销
     * @param userId
     * @return
     */
    @PostMapping("/logout")
    public Rsp logout(@RequestHeader(value = "userId") String userId) {
        return userService.logout(userId);
    }

    @GetMapping("/getNextLevelArea")
    public Rsp getNextLevelArea(String parentCode){
        return userService.getNextLevelArea(parentCode);
    }

    @GetMapping("/getOneLevelArea")
    public Rsp getOneLevelArea(Integer level){
        return userService.getOneLevelArea(level);
    }

    //修改密码
    @PostMapping("/updatePwd")
    public Rsp updatePwd(@RequestBody User user,@RequestHeader("token") String token){
        return userService.updatePwd(user.getId(),user.getPassword());
    }

    @PostMapping("/testmq")
    public String test(){
        rabbitTemplate.convertAndSend("exchange_pushmsg","rk_pushmsg","50000");
        return "成功";
    }
}

